import Vue from "vue";
import VueRouter from "vue-router";
import $api from '@/http/api.js'
import store from '@/store';

Vue.use(VueRouter);
import Login from "../views/login/Login.vue";
import Register from "../views/register/Register.vue";
import Index from "../views/index/Index.vue";
import StudentsList from "../components/students/StudentsList.vue";
import SubjectsList from "../components/students/SubjectsList.vue";
import CourseList from "../components/students/CourseList";
import Chart from "../components/students/Chart.vue";
import ClassList from '../components/students/ClassList.vue'
import TeacherList from '../components/students/TeacherList'
import Add from '../components/students/Add.vue'
//一般不常进入的页面会使用懒加载
const Error = () => import("../views/404/Error.vue");



const routes = [
  {
    path: "/",
    redirect: "/login" //重定向
  },
  {
    path: "/login",
    name: "Login",
    component: Login
  },
  {
    path: "/register",
    name: "Register",
    component: Register
  },
  {
    path: "/index",
    redirect: "/index/chart",
    name: "Index",
    component: Index,
    beforeEnter: (to, from, next) => {
      const token = localStorage.token;
      if (token) {
        // 判断 token 是否过期
        // 发送“获取用户信息”的请求，将 token 携带到后端验证有效期
        $api.userApi.getUserInfo().then(({ data }) => {
          if (data.code) {
            store.commit('GetUserInfo', data.data);
            next();
          }
        })
      } else {
        store._vm.$message.error('你还未登录，请先登录')
        next('/login');
      }
    },
    children: [
      {
        path: "chart",
        name: "chart",
        component: Chart,
        meta: {
          iskeepalive: false,
          breadTitle: '学生列表'
        }
      },
      {
        path: "studentsList",
        name: "StudentsList",
        component: StudentsList,
        meta: {
          iskeepalive: false,
          breadTitle: '学生列表'
        }
      },
      {
        path: "add",
        name: "Add",
        component: Add,
        meta: {
          iskeepalive: true,
          breadTitle: '新增'
        }

      },
      {
        path: "classList",
        name: "classList",
        component: ClassList,
        meta: {
          iskeepalive: false,
          breadTitle: '班级列表'
        }
      }
      ,
      {
        path: "SubjectsList",
        name: "SubjectsList",
        component: SubjectsList,
        meta: {
          iskeepalive: false,
          breadTitle: '学科列表'
        }
      },
      {
        path: "CourseList",
        name: "CourseList",
        component: CourseList,
        meta: {
          iskeepalive: false,
          breadTitle: '学科列表'
        }
      },
      {
        path: "TeacherList",
        name: "TeacherList",
        component: TeacherList,
        meta: {
          iskeepalive: false,
          breadTitle: '学科列表'
        }
      }
    ]
  },
  {
    path: "*",
    name: "Error",
    component: Error
  }
];

const router = new VueRouter({
  routes: routes, //路由配置
  // mode: 'history'

});

export default router;
